// pages/classify/index.js
import images from '../../utils/images.js'
const app = getApp()
Page({
  data: {
    ...app.globalData.config,
    tabList: [
      {name: '礼品系列', key: '1'},
      {name: '花茶系列', key: '2'},
      {name: '办公用茶', key: '3'},
      {name: '茶具茶生活', key: '4'},
      {name: '存茶(收藏)', key: '5'},
      {name: '预定款', key: '6'},
    ],
    current: '1',
    offset: '0rpx',
    offsetTimeout: null,
    list: [
      {name: '茶盘', url: images.classify1},
      {name: '茶荷', url: images.classify2},
      {name: '烧水壶', url: images.classify3},
      {name: '茶巾', url: images.classify4},
      {name: '茶盘', url: images.classify1},
      {name: '茶荷', url: images.classify2},
      {name: '烧水壶', url: images.classify3},
      {name: '茶巾', url: images.classify4},
    ]
  },
  getOffset(){
      const offsetTimeout = setTimeout(() => {
        let query = wx.createSelectorQuery();
        query.select('.header').boundingClientRect(rect=>{
          try{
            let clientHeight = rect.height;
            let clientWidth = rect.width;
            let ratio = 750 / clientWidth;
            let height = clientHeight * ratio;
            this.setData({
              offset: `${height||0}rpx`
            })
          }catch(e){
            console.log("ERROR:获取分类页头部高度失败")
          }
        }).exec();
      }, 300)
      this.setData({offsetTimeout})
    
  },
  
  getRuleList(){
    var that = this;
    wx.request({
      url: 'http://localhost:8080/setting/dict/list/constant/product_rule',
      success: function(result) {
        that.setData({
          tabList: result.data.data
        });
        that.data.tabList = result.data;
      }
    })
  },

  getSubRuleList(parentId) {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/setting/dict/list/parent-id/' + parentId,
      success: function(res) {
        var result = res.data;
        if (result.code = 'success' && result.data != null && result.data.length > 0) {
          var itemList = [];
          result.data.forEach(it => {
            itemList.push(it.dictName);
          });

          wx.showActionSheet({
            itemList: itemList,
            success (res) {
              console.log(res.tapIndex)
              console.log(result.data[res.tapIndex])
              that.searchProduct({ruleIdList:[result.data[res.tapIndex].dictId]});
            },
            fail (res) {
              console.log(res.errMsg)
            }
          })
        }       
      }
    })
  },

  searchProduct(condition) {
    console.log(condition)
    var that = this;
    wx.request({
      method:'post',
      url: 'http://localhost:8080/bu/product/search',
      data: condition,
      success: function(res) {
        // 清空原来的数据
        that.setData({
          list:[]
        });

        var result = res.data;
        if (result.code = 'success' && result.data != null) {
          var productList = [];
          result.data.forEach(it => {
            it.url = '/images/resource/icon_classify_1.png';
            productList.push(it);
            
          });
          that.setData({
            list: productList
          });
        }
      }
    })
  },

  handleChange({ detail }){
    this.setData({
      current: detail.key
    })
    this.getSubRuleList(detail.key);
  },

  toProdunctDetail(ev) {
    console.log(ev)
    
    let info = ev.currentTarget.dataset;

        let navigateUrl = '/pagesProduct/index?';

        for (let key in info) {
            info[key] = encodeURIComponent(info[key]);
            navigateUrl += key + '=' + info[key] + '&';
        }

        navigateUrl = navigateUrl.substring(0, navigateUrl.length - 1);

        wx.navigateTo({
            url: navigateUrl
        });
  },

  onLoad(){
    setTimeout(()=>{
      this.selectComponent("#page").hideLoading()
      this.getOffset() // 计算头部高度
    },2000);

    this.getRuleList();
  },
  onShow(){
    this.getOffset() // 计算头部高度
  },
  onUnload(){
    if(this.data.offsetTimeout) clearTimeout(this.data.offsetTimeout)
  }
})